package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.mission.schedule.R;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.NoteTypeBean;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.service.NoteService;
import com.mission.schedule.utils.NetUtil;
import com.mission.schedule.utils.ProgressUtil;
import com.mission.schedule.utils.ScreenUtils;
import com.mission.schedule.utils.SharedPrefUtil;
import com.mission.schedule.utils.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 重复北背景颜色
 * Created by liwei on 2017/12/1.
 */

public class RepeatBgColorMenuDialog extends Dialog {


    CallBack callBack;

    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;

    String Color = "";
    int itemWidth = 0;

    public RepeatBgColorMenuDialog(@NonNull final Context context, @StyleRes final int themeResId,
                                   final WindowManager windowManager, final String ShareFileType) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;

        setCanceledOnTouchOutside(true);
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        Color = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFileType, "1");
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_repeat_bg_color_menu, null);
        setContentView(view);

        RelativeLayout color1 = (RelativeLayout) view.findViewById(R.id.color1);
        RelativeLayout color2 = (RelativeLayout) view.findViewById(R.id.color2);
        RelativeLayout color3 = (RelativeLayout) view.findViewById(R.id.color3);
        RelativeLayout color4 = (RelativeLayout) view.findViewById(R.id.color4);
        int b = ScreenUtils.getScreenWidth(context);
        int c = Utils.dipTopx(context, 100);
        itemWidth = (ScreenUtils.getScreenWidth(context) - 30 - Utils.dipTopx(context, 100)) / 3;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(itemWidth, itemWidth);
        params.setMargins(Utils.dipTopx(context, 20),0,0,0);
        color1.setLayoutParams(params);
        color2.setLayoutParams(params);
        color3.setLayoutParams(params);
        color4.setLayoutParams(params);

        ImageView check1 = (ImageView) view.findViewById(R.id.check1);
        ImageView check2 = (ImageView) view.findViewById(R.id.check2);
        ImageView check3 = (ImageView) view.findViewById(R.id.check3);
        ImageView check4 = (ImageView) view.findViewById(R.id.check4);

        switch (Color) {
            case "1":
                check1.setVisibility(View.VISIBLE);
                break;
            case "2":
                check2.setVisibility(View.VISIBLE);
                break;
            case "3":
                check3.setVisibility(View.VISIBLE);
                break;
            case "4":
                check4.setVisibility(View.VISIBLE);
                break;
        }


        view.findViewById(R.id.color1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFileType, "1");
                callBack.color1();
                dismiss();
            }
        });
        view.findViewById(R.id.color2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFileType, "2");
                callBack.color2();
                dismiss();
            }
        });
        view.findViewById(R.id.color3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFileType, "3");
                callBack.color3();
                dismiss();
            }
        });
        view.findViewById(R.id.color4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFileType, "4");
                callBack.color4();
                dismiss();
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth() - 30;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    public interface CallBack {
        void color1();
        void color2();
        void color3();
        void color4();
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

}
